package com.liuyk.commonlib;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.fragment.BaseLoadingFragment;
import com.liuyk.commonlib.web.APPWebView;
import com.liuyk.commonlib.web.bridge.BasisView;

/**
 * 封装WebView
 * <p>
 * Created by liuyakui on 2020/5/21.
 */
public class WebViewFragment extends BaseLoadingFragment implements BasisView {
    private APPWebView mWebView;
    private Bundle arguments;
    private String url;

    private Object jsInterface;
    private String jsName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arguments = getArguments();
        if (arguments != null) {
            url = arguments.getString("url");
        }
    }

    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup container) {
        View view = super.getContentView(inflater, container);
        setLoadingContent(R.layout.base_fragment_web_view);
        initView(view);
        return view;
    }

    @SuppressLint("JavascriptInterface")
    private void initView(View view) {
        mWebView = view.findViewById(R.id.web_view);
        mWebView.setWebViewFragment(this);
        mWebView.setBasisView(this);
        mWebView.addJavascriptInterface(jsInterface, jsName);
        mWebView.loadUrl(url);
    }

    @Override
    protected void createView() {

    }

    @Override
    protected void onRetry() {

    }

    public void setJsInterface(Object o, String name) {
        jsInterface = o;
        jsName = name;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mWebView.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mWebView.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public Context getTarget() {
        return getActivity();
    }

    @Override
    public WebView getWebView() {
        return mWebView;
    }

    @Override
    public String onExtraBridge(String bridgeName, String action, boolean isSync, String callbackId, String callbackFunction, String params) {
        return "";
    }

    @Override
    public void eventDispatch(String eventDispatchFunction, String event, ValueCallback callback) {

    }

    @Override
    public void onJsCallback(String extra, String callbackFunction, String response) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mWebView != null) {
                        mWebView.evaluateJavascript(callbackFunction, response, null);
                    }
                }
            });
        }
    }

    @Override
    public <T> T getArg(String key, T defaultValue) {
        return null;
    }

    @Override
    public <T> void putArg(String key, T value) {

    }

    @Override
    public void removeArg(String key) {

    }

    @Override
    public void setTitle(String title) {

    }

    @Override
    public void closeView() {

    }

    @Override
    public void showTitleRightButtons(String callbackId, String callbackFunction, String params) {

    }

    @Override
    public void exeRoute(String url, String callbackId, String callbackFunction) {

    }

    @Override
    public void openPicture(String imageUrl, int x, int y, int width, int height) {

    }

    @Override
    public void setHeadBackground(String color) {

    }

    @Override
    public void onLoadStart(String url) {

    }

    @Override
    public void onProgressChanged(int progress, String url) {
        if (progress == 100) {
            showContentState();
        }
    }
}
